package com.aisino.nmg.task;

import com.aisino.nmg.ant.env.Environment;
import com.aisino.nmg.ant.task.AbstractTask;
import com.aisino.nmg.ant.task.GeneratorToolTask;
import com.aisino.nmg.constant.GeneratorConstants;
import com.aisino.nmg.template.EngineBuilder;
import com.aisino.nmg.template.TemplateEngine;
import com.aisino.nmg.util.FileUtil;
import com.aisino.nmg.util.PathUtil;
import com.aisino.nmg.util.StringUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class InitProjectFrameTask extends AbstractTask {

	public InitProjectFrameTask(GeneratorToolTask parent) {
		super(parent);
	}

	@Override
	public String getName() {
		return "SSM 框架初始化";
	}

	@Override
	public Integer getSort() {
		return new Integer(2);
	}

	@Override
	public void execute() {
		Environment environment = this.getParent().getEnvironment();
		String PROJECT_ROOT_DIR = environment.getGeneratorDir() + this.getParent().getProject().getArtifactId();
		FileUtil.createFilePath(PROJECT_ROOT_DIR);
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubWebProjectName() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubWebProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.ACTION_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubWebProjectName() + GeneratorConstants.WEBAPP_PATH+GeneratorConstants.WEB_INF);
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubServiceProjectName() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubServiceProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.SERIVCE_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubServiceProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.SERIVCE_IMPL_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName() + GeneratorConstants.RESOURCES_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.MAPPER_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.MAPPER_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.MODEL_PACKAGE));
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubCommonProjectName() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		FileUtil.createFilePath(
				PROJECT_ROOT_DIR + File.separator + environment.getSubCommonProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId()));

		EngineBuilder engineBuilder = new EngineBuilder(PathUtil.getCurrentProjectPath() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		TemplateEngine templateEngine = engineBuilder.getTemplateEngine("freemarker");
		FileUtil.createFilePath(PROJECT_ROOT_DIR, "pom.xml");
		FileUtil.createFilePath(PROJECT_ROOT_DIR + File.separator + environment.getSubWebProjectName(), "pom.xml");
		FileUtil.createFilePath(PROJECT_ROOT_DIR + File.separator + environment.getSubServiceProjectName(), "pom.xml");
		FileUtil.createFilePath(PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName(), "pom.xml");
		FileUtil.createFilePath(PROJECT_ROOT_DIR + File.separator + environment.getSubCommonProjectName(), "pom.xml");

		Map<String, Object> model = new HashMap<>();
		model.put("artifactId", this.getParent().getProject().getArtifactId());
		model.put("groupId", this.getParent().getProject().getGroupId());

		templateEngine.process(model, "ssm" + File.separator + "pom.ftl", FileUtil.writeFileToPath(PROJECT_ROOT_DIR, "pom.xml"));
		templateEngine.process(model, "ssm" + File.separator+"common-pom.ftl", FileUtil.writeFileToPath(PROJECT_ROOT_DIR + File.separator + environment.getSubCommonProjectName(), "pom.xml"));
		templateEngine.process(model, "ssm" + File.separator+"dao-pom.ftl", FileUtil.writeFileToPath(PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName(), "pom.xml"));
		templateEngine.process(model, "ssm" + File.separator+"service-pom.ftl", FileUtil.writeFileToPath(PROJECT_ROOT_DIR + File.separator + environment.getSubServiceProjectName(), "pom.xml"));
		templateEngine.process(model, "ssm" + File.separator+"web-pom.ftl", FileUtil.writeFileToPath(PROJECT_ROOT_DIR + File.separator + environment.getSubWebProjectName(), "pom.xml"));
	}
}
